/*
 * GoodsDetailInfoFragment.java
 * business
 *
 * Created by SunDing on 2016/9/28.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.goodsdetail.value;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.response.GoodsDetailValueResponse;
import com.yidiandao.business.manager.ImageManager;
import com.yidiandao.business.ui.widget.recyclerview.LoadMoreRecyclerContainer;
import com.yidiandao.business.ui.widget.recyclerview.MultiTypeLoadMoreAdapter;
import com.yidiandao.business.ui.widget.recyclerview.provider.GoodsDetailValueProvider;
import com.yidiandao.business.ui.widget.view.GoodsDetailValueTitleBar;
import com.yidiandao.business.utils.AnimatorUtils;
import com.yidiandao.business.utils.SysUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;

import static com.yidiandao.business.ui.widget.view.GoodsDetailValueTitleBar.POSITION.VALUE_ALL;
import static com.yidiandao.business.ui.widget.view.GoodsDetailValueTitleBar.POSITION.VALUE_GOOD;
import static com.yidiandao.business.ui.widget.view.GoodsDetailValueTitleBar.POSITION.VALUE_MID;
import static com.yidiandao.business.ui.widget.view.GoodsDetailValueTitleBar.POSITION.VALUE_PIC;
import static com.yidiandao.business.ui.widget.view.GoodsDetailValueTitleBar.POSITION.VALUE_WORSE;

/** 商品评价 */
public class GoodsDetailValueFragment extends SuperBaseFragment
        implements GoodsDetailValueContract.View {

    private GoodsDetailValueContract.Presenter mPresenter;

    private RecyclerView mRecyclerView;

    private GoodsDetailValueTitleBar mTitleBar;

    private LoadMoreRecyclerContainer mLoadMoreContainer;

    private ImageView mIvToTop;

    private boolean isVisibility;

    private boolean isOpening, isHiding;

    private MultiTypeLoadMoreAdapter mAdapter;

    private String mGoodsID;

    private int index; //点击的位置

    public static GoodsDetailValueFragment getInstance() {
        return new GoodsDetailValueFragment();
    }

    @Override
    public void setPresenter(GoodsDetailValueContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return GoodsDetailValueFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    protected void initViews() {

        mLoadMoreContainer =
                (LoadMoreRecyclerContainer) mRootView.findViewById(R.id.load_more_container);
        mTitleBar = (GoodsDetailValueTitleBar) mRootView.findViewById(R.id.title_bar);
        mIvToTop = (ImageView) mRootView.findViewById(R.id.iv_to_top);

        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));

        mAdapter = new MultiTypeLoadMoreAdapter();
        mAdapter.register(
                GoodsDetailValueResponse.ValueBean.class,
                new GoodsDetailValueProvider(getActivity()));

        mLoadMoreContainer.setRecyclerViewAdapter(mAdapter);

        initEvent();
    }

    private void initEvent() {
        // 上拉加载更多
        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        mPresenter.loadMore(mGoodsID, index, false);
                    }
                });
        mTitleBar.setOnBarItemClickListener(
                new GoodsDetailValueTitleBar.OnBarItemClickListener() {
                    @Override
                    public void onBarItemClick(int POSITION) {
                        switch (POSITION) {
                            case VALUE_ALL:
                                index = 0;
                                break;
                            case VALUE_GOOD:
                                index = 1;
                                break;
                            case VALUE_MID:
                                index = 2;
                                break;
                            case VALUE_WORSE:
                                index = 3;
                                break;
                            case VALUE_PIC:
                                index = 4;
                                break;
                        }
                        mPresenter.loadMore(mGoodsID, index, true);
                    }
                });

        mLoadMoreContainer.setOnScrollListener(
                new RecyclerView.OnScrollListener() {
                    private int totalDy;

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        totalDy -= dy;
                        if (dy > 0) {
                            // 动画隐藏
                            hideAnimator(mIvToTop);
                        } else {
                            if (Math.abs(totalDy) > SysUtils.getScreenHeight(
                                    UIUtils.getContext())) {
                                // 超过屏幕高度，开始显示
                                // 动画显示
                                showAnimator(mIvToTop);
                            } else {
                                // 隐藏
                                // 动画隐藏
                                hideAnimator(mIvToTop);
                            }
                        }
                    }
                });

        mIvToTop.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 返回顶部
                        mRecyclerView.smoothScrollToPosition(0);
                    }
                });
    }

    /** 显示动画 */
    private void showAnimator(View view) {
        AnimatorUtils.showToTopAnimator(
                view,
                isVisibility,
                isOpening,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isOpening = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        isVisibility = true;
                        isOpening = false;
                    }
                });
    }

    /** 隐藏动画 */
    private void hideAnimator(final View view) {
        AnimatorUtils.hideToTopAnimator(
                view,
                isVisibility,
                isHiding,
                new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        isHiding = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        view.setVisibility(View.GONE);
                        isVisibility = false;
                        isHiding = false;
                    }
                });
    }

    @Override
    protected void startLoadData() {
        if (getUserVisibleHint()) mPresenter.loadMore(mGoodsID, index, true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_goods_detail_recommend;
    }

    @Override
    public void setGoodsList(List<GoodsDetailValueResponse.ValueBean> data) {
        mAdapter.setItems(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    public void setGoodsInfo(String goodsID) {
        mGoodsID = goodsID;
        mPresenter.loadMore(mGoodsID, index, true);
    }

    @Override
    public void setTitleBarData(
            String allNum, String goodNum, String midNum, String worseNum, String picNum) {
        mTitleBar.setData(allNum, goodNum, midNum, worseNum, picNum);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ImageManager.getInstance().clearList();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }
}
